package BanyuanClub.Study.Day0317;

import javax.naming.Name;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MyClass {
    @MyAnno(name="aaa")
    private String name;

    @MyAnno(age=20)
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) throws Exception{
        Class clazz = MyClass.class;
        Field nameField = clazz.getDeclaredField("name");

        //得到name属性上的注解对象
        MyAnno myAnno = nameField.getAnnotation(MyAnno.class);

        //调用注解对象的方法来取得注解里属性的值
        String myAnnoName = myAnno.name();

        //得到setName方法
        Method setNameMehtod = clazz.getMethod("set"+
                "name".substring(0,1).toUpperCase()
                +"name".substring(1),String.class);
        //该类的构造器
        Constructor constructor = clazz.getConstructor();
        //调用无参构造器生成了一个对象
        Object obj = constructor.newInstance();

        //调用该方法
        setNameMehtod.invoke(obj,myAnnoName);

        //得到name属性的值
        System.out.println(nameField.get(obj));
    }
}
